package com.ruslan.magaz.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ruslan.magaz.domain.Cart;
import com.ruslan.magaz.domain.Order;
import com.ruslan.magaz.domain.OrderStatus;
import com.ruslan.magaz.domain.OrderStatusName;
import com.ruslan.magaz.domain.OrderedItem;
import com.ruslan.magaz.domain.Product;
import com.ruslan.magaz.domain.User;
import com.ruslan.magaz.repository.ItemDao;
import com.ruslan.magaz.repository.OrderDao;

@Service
public class OrderServiceImpl implements OrderService{
	
	
	@Autowired
    private  OrderDao orderDao;
	@Autowired
    private ItemDao itemDao;
	
	@Override
	@Transactional
	public void addOrder(int userId, Order order) {
		orderDao.addOrder(userId, order);
		for (OrderedItem orderedItem: order.getOrderedItems()){
			Product product = itemDao.getProductById(orderedItem.getProduct().getProductId());
			int count = product.getStock() - orderedItem.getProductQuantity();
			product.setStock(count);
		}
	}

	@Override
	@Transactional
	public void removeOrder(Order order) {
		orderDao.removeOrder(order);
		
	}

	@Override
	@Transactional
	public void editOrder(Order order) {
		orderDao.editOrder(order);
		
	}

	@Override 
	@Transactional
	public Order getOrderById(int id) {
		return orderDao.getOrderById(id);
	}

	@Override
	@Transactional
	public List<Order> getAllOrdersForUser(int userId, int pageNum, int pageSize) {
		return orderDao.getAllOrdersForUser(userId, pageNum, pageSize);
	}

	@Override 
	@Transactional
	public void addOrderedItem(int orderId, OrderedItem orderedItem) {
		orderDao.addOrderedItem(orderId, orderedItem);
	}

	@Override
	@Transactional
	public void removeOrderedItem(OrderedItem orderedItem) {
		orderDao.removeOrderedItem(orderedItem);
	}

	@Override
	@Transactional
	public void editOrderedItem(OrderedItem orderItemId) {
		orderDao.editOrderedItem(orderItemId);
	}

	@Override
	@Transactional
	public OrderedItem getOrderedItemById(int orderedId) {
		return orderDao.getOrderedItemById(orderedId);
	}

	@Override
	@Transactional
	public List<OrderedItem> getAllOrderedItemsForOrder(int orderId, int pageNum, int pageSize) {
		return orderDao.getOrderedItemsForOrder(orderId, pageNum, pageSize);
	}

	@Override
	@Transactional
	public void addOrderStatus(int orderId, OrderStatus orderStatus) {
		orderDao.addOrderStatus(orderId, orderStatus);
	}

	@Override
	@Transactional
	public void removeOrderStatus(OrderStatus orderStatus) {
		orderDao.removeOrderStatus(orderStatus);
	}

	@Override
	@Transactional
	public void editOrderStatus(OrderStatus orderStatus) {
		orderDao.editOrderStatus(orderStatus);		
	}

	@Override
	@Transactional
	public OrderStatus getOrderStatusById(int id) {
		return orderDao.getOrderStatusById(id);
	}

	@Override
	@Transactional
	public List<OrderStatus> getOrderStatusesForOrder(int orderId) {
		return orderDao.getOrderStatusesForOrder(orderId);
	}

	@Override
	@Transactional
	public OrderStatusName getOrderStatusNameById(int id) {
		return orderDao.getOrderStatusNameById(id);
	}

	@Override
	public void setInitialStatus(Order order) {
		OrderStatus os = new OrderStatus();
		os.setOrder(order);
		os.setOrderStatusName(new OrderStatusName(INITIAL_STATUS_ID));
		os.setPlace(INITIAL_PLACE);
		os.setStatusTime(new Date());
		List<OrderStatus> osList = new ArrayList<OrderStatus>();
		osList.add(os);
		order.setOrderStatuses(osList);
	}

	@Override
	public int calculateDiscount(int userId) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	@Transactional
	public List getOrderLines(int userId, int pageNum, int pageSize) {
		return orderDao.getOrderLines(userId, pageNum, pageSize);
	}
	
	@Override
	@Transactional
	public List<OrderedItem> getOrderedItemsForOrder(int orderId, int pageNum, int pageSize) {
		return orderDao.getOrderedItemsForOrder(orderId, pageNum, pageSize);
	}

	@Override
	@Transactional
	public int ordersForUserPageCount(int userId, int pageSize) {
		return orderDao.ordersForUserPageCount( userId, pageSize);
	}

	@Override
	@Transactional
	public int orderedItemsForOrderPageCount(int orderId, int pageSize) {
		return orderDao.orderedItemsForOrderPageCount( orderId, pageSize);
	}

	@Override
	@Transactional
	public User getUsersforOrder(int orderId) {
		return orderDao.getUsersforOrder(orderId);
	}

	@Override
	@Transactional
	public List<OrderStatusName> getAllOrderStatusNames() {
		return orderDao.getAllOrderStatusNames();
	}
	
}
